/**
 * Created by Administrator on 2020/11/10 0010.
 */

(function ($, doc, win, undefined) {

    var dataJson = {
        "product_title": "这是一个测试产品",
        "type": 2,
        "suit": [
           /* {
                "title": "A套餐",
                "suit_id": 1,
                "price": [
                    {
                        "id": 1,
                        "date": "2023-12-09",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {

                        "date": "2023-12-10",
                        "not_set": true,
                        "price": "",
                        "basic_price": "",
                        "stock": ""
                    },
                    {
                        "date": "2023-12-10",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-03",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-04",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-05",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-06",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-07",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-02",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-03",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-04",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    }
                ]
            },
            {
                "title": "B套餐",
                "suit_id": 2,
                "price": [
                    {
                        "id": 1,
                        "date": "2023-12-09",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {

                        "date": "2023-12-10",
                        "not_set": true,
                        "price": "",
                        "basic_price": "",
                        "stock": ""
                    },
                    {
                        "date": "2023-12-10",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-03",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-04",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-05",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-06",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-07",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-02",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-03",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-04",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    }
                ]
            },
            {
                "title": "c套餐",
                "suit_id": 3,
                "price": [
                    {
                        "id": 1,
                        "date": "2023-12-09",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {

                        "date": "2023-12-10",
                        "not_set": true,
                        "price": "",
                        "basic_price": "",
                        "stock": ""
                    },
                    {
                        "date": "2023-12-10",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-03",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-04",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-05",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-06",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-07",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    },
                    {
                        "date": "2020-11-02",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-03",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100
                    },
                    {
                        "date": "2020-11-04",
                        "price": "501.00",
                        "basic_price": "400.00",
                        "stock": 100,
                        "is_open": 1
                    }
                ]
            }*/
        ]
    };

    var currDT = new Date();
    var dateGroup = [];
    var ArrWeek = ["日", "一", "二", "三", "四", "五", "六"]; //显示星期
    var selectType = 3;

    var curYear = currDT.getFullYear();
    var curMonth = currDT.getMonth() + 1;

    //重新赋值
    var lastDay; //本周的最后一天
    var firstDay; //本周的第一天

    //取得当前日期一周内的某一天
    function getWeek(i) {
        var now = new Date(currDT);
        var n = now.getDay();
        var start = new Date(now);
        start.setDate(now.getDate() - n + i); //取得一周内的第一天、第二天、第三天...
        return start;
    }

    //取得当前日期一周内的七天
    function getDays() {
        var days = [];
        for (var i = 1; i <= 7; i++) {
            days[i - 1] = getWeek(i);
        }
        return days;
    }

    //指定日期的下一周(后七天)
    function getNextWeek(dt, i) {
        var today = dt;
        today.setDate(today.getDate() + i);
        return today;
    }

    //取得下一周的日期数(共七天)
    function getNextWeekDatas(ndt) {
        var days = [];
        for (var i = 1; i <= 7; i++) {
            var dt = new Date(ndt);
            days[i - 1] = getNextWeek(dt, i);
        }
        return days;
    }

    //指定日期的上一周(前七天)
    function getPreviousWeek(dt, i) {
        var today = dt;
        today.setDate(today.getDate() + i);
        return today;
    }


    //取得上一周的日期数(共七天)
    function getPreviousWeekDatas(ndt) {
        var days = [];
        for (var i = -7; i <= -1; i++) {
            var dt = new Date(ndt);
            days[7 + i] = getPreviousWeek(dt, i);
        }
        return days;
    }

    /**
     * 日期格式化
     * @param d
     * @param format
     * @returns {string}
     * @constructor
     */
    function DateFormat(d, format) {

        var year = d.getFullYear();
        var month = d.getMonth() + 1;
        month = month < 10 ? '0' + month : month;
        var date = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
        var dateStr;
        if(format === "mm-dd")
        {
            dateStr = month + "-" + date;
        }else{
            dateStr = year + "-" + month + "-" + date;
        }
        return dateStr;
    }

    function getDateArea(dateStr, dayNum){
        var cureentDate = new Date(dateStr);
        var endDateStr = new Date(cureentDate.setDate(cureentDate.getDate() + dayNum));
        var endYear = endDateStr.getFullYear();
        var endMonth = endDateStr.getMonth() + 1;
        var endDate = endDateStr.getDate();
        return {
            year: endYear,
            month: endMonth,
            day: endDate
        };
    }


    function initPrice(startDate) {
        dateGroup = [];
        var gd, dNum = 30;
        //在表格显示一月日期
        for (var j = 0; j <= dNum; j++) {
            var dateArea = getDateArea(startDate, j);
            var year = dateArea['year'], month = dateArea['month'], day = dateArea['day'];
            gd = ArrWeek[new Date(year + '/' +month + '/' + day).getDay()];
            var currentDate = (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
            dateGroup.push({
                date: currentDate,
                week: gd
            })
        }

        //重新赋值
        lastDay = getDays()[6]; //本周的最后一天
        firstDay = getDays()[0]; //本周的第一天
    }


    var Calendar = function (el, opt) {
        this.$el = el;
        this.defaults = {
            type: 1,
            data: dataJson,
            startDate: "2023-12-05",
            onShow: function () {
            },
            handle: function () {
            },
            onChangeDate: function () {
                
            }
        };

        this.options = $.extend({}, this.defaults, opt);
    };

    Calendar.prototype = {

        init: function () {
            initPrice(this.options.startDate);
            this.createElement();
            this.createTable();
            this.createPrice();
           // this.createControl();
            this.createCalendar(dateGroup);
        },

        createTable: function () {
            var html = '<table class="calendar-wrap-table">' +
                '</table>';
            this.$el.append(html);
        },

        createCalendar: function (dateGroup) {
            $('.calendar-wrap-table thead').remove();
            var html = '<thead><tr>';
            for (var i = 0; i < dateGroup.length; i++) {
                html += '<th class="calendar-item__hd calendar-item calendar-item__data">' +
                    '<div class="date-group"><div class="week-label">' + dateGroup[i].week + '</div><div class="date-label">' + dateGroup[i].date + '</div></div>' +
                    '</th>'
            }
            html += '</tr></thead>';
            $('.calendar-wrap-table').prepend(html)
        },

        createElement: function () {
            var startDate = this.options.startDate;
            var startDateArea = getDateArea(startDate, 0);
            var endDateArea = getDateArea(startDate, 30);
            var cYear = startDateArea.year;
            var cMonth = startDateArea.month;
            var cDate = startDateArea.day;
            cMonth = cMonth > 9 ? cMonth : "0" + cMonth;
            cDate = cDate > 9 ? cDate : "0" + cDate;
            var endYear = endDateArea.year;
            var endMonth = endDateArea.month;
            var endDate = endDateArea.day;
            endMonth = endMonth > 9 ? endMonth : "0" + endMonth;
            endDate = endDate > 9 ? endDate : "0" + endDate;
            var year = cYear !== endYear ? cYear +"-"+ endYear : cYear;

            // 计算向前日期
            var prevDateArea = getDateArea(startDate, -31),
                prevDate = prevDateArea.year + "-" + prevDateArea.month + "-" + prevDateArea.day;

            // 计算向后日期
            var nextDateArea = getDateArea(startDate, 31),
                nextDate = nextDateArea.year + "-" + nextDateArea.month + "-" + nextDateArea.day;

            var html = '';
            html += '<table class="calendar-side-table calendar-table">' +
                '<thead>' +
                '<tr>' +
                '<th colspan="2" class="sidebar-item__hd calendar-item">' +
                '<div class="sidebar-item__bar">' +
                '<div class="calendar-date-change">' +
                '<div class="calendar-date-prev calendar-date-change-btn" data-date="'+prevDate+'"></div>' +
                '<div class="calendar-date-change__item">' +
                '<div class="calendar-date-change__item__md">'+cMonth+'-'+cDate+'~'+endMonth+'-'+endDate+'</div>' +
                '<div class="calendar-date-change__item__year">'+year+'</div>' +
                '</div>' +
                '<div class="calendar-date-next calendar-date-change-btn" data-date="'+nextDate+'"></div>' +
                '</div></div>' +
                '</th>' +
                '</tr>' +
                '</thead>' +
                '<tbody>';

            for (var i = 0; i < this.options.data.suit.length; i++) {
                html += '<tr>' +
                    '<td rowspan="1" colspan="1" class="calendar-item sidebar-item__sub"><div class="sidebar-item__sub__title">' + this.options.data.suit[i].title + '</div></td>' +
                    '</tr>';
            }
            html += '</tbody>' +
                '</table>';

            var table = this.$el.find('.calendar-side-table');
            if (table) {
                table.remove();
            }
            this.$el.prepend(html);

            var self = this;
            // 监听日期点击时间
            $(".calendar-date-change-btn").on('click', function(e) {
                var date = $(this).data("date")
                self.options.onChangeDate(date);
            })
        },

        createPrice: function () {
            let priceStockStr;
            var self = this;
            $('.calendar-wrap-table tbody').remove();
            var html = '<tbody>', item, price, stock,isOpen,basicPrice;
            for (var i = 0; i < this.options.data.suit.length; i++) {
                html += '<tr>';
                for (var j = 0; j < this.options.data.suit[i].price.length; j++) {
                    item = this.options.data.suit[i].price[j];
                    if (item.not_set) {
                        priceStockStr = '<p class="text-grey">空</p>';
                    } else {

                        isOpen = item.is_open == 1 ? '<p class="text-grey calendar-price-open">开</p>' : '<p class="text-grey calendar-price-close">关</p>';
                        price = item.price ? '<p class="text-grey calendar-price">&yen; ' + item.price + '</p>' : '<p class="text-grey">空</p>';
                        basicPrice = item.basic_price ? '<p class="text-grey calendar-price">&yen; ' + item.basic_price + '</p>' : '<p class="text-grey">空</p>';
                        stock = item.price && item.stock !== '' ? (parseFloat(item.stock) === -1 ? '<p class="calendar-stock">不限</p>' : '<p class="calendar-stock">' + item.stock + '</p>') : '';
                        priceStockStr = isOpen + price + basicPrice + stock;
                    }
                    html += '<td class="calendar-item  sidebar-item__sub calendar-item__data calendar-item__date" data-col="' + i + '" data-row="' + j + '">'+ priceStockStr + '</td>';
                }
                html += '</tr>';
            }
            html += '</tbody>';

            $('.calendar-wrap-table').append(html);
            // 单元格回调
            $('.calendar-item__date').on('click', function () {
                var col = $(this).data('col'),
                    row = $(this).data('row'),
                    data = self.options.data.suit[col].price[row];
                data.title = self.options.data.suit[col].title;
                data.ticket_id = self.options.data.suit[col].ticket_id;
                data.sku_id = self.options.data.suit[col].sku_id;
                self.options.onShow(data);
            });
        },

        createControl: function(){
            var html = '<table class="table-control"><thead><tr><td class="calendar-item__hd control-item">操作</td></tr></thead><tbody>';
            for (var i = 0; i < this.options.data.price.length; i++) {
                html += '<tr><td class="control-item"><a href="javascript:;" class="control-set-label text-decoration-none">设置</a></td></tr>';
            }
            html += '</tbody></table>';
            // this.$el.append(html);
        },

        setDate: function (res) {
            currDT = res;
            initPrice();
            this.createCalendar(dateGroup);
        },

        setMonth: function (res) {
            var d = new Date(res);
            curYear = d.getFullYear();
            curMonth = d.getMonth() + 1;
            initPrice();
            this.createCalendar(dateGroup);
        },

        setData: function (res) {
            this.options.data = res;
            this.createElement()
            this.createPrice()
        },

        setStartDate: function (startDate) {
            this.options.startDate = startDate;
            initPrice(startDate);
            this.createCalendar(dateGroup);
        }

    };

    $.fn.calendarPrice = function (options) {
        var calendar = new Calendar(this, options);
        calendar.init();
        return calendar
    };

})(jQuery, document, window, undefined);

